﻿using System.Collections;
using System.Collections.Generic;
using GameFramework.Event;
using GameFramework.Fsm;
using GameFramework.Procedure;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Client {
    public class ProcedurePreload : ProcedureBase
    {
        /// <summary>
        /// 定义需要读取的数据表
        /// </summary>
        public static readonly string[] DateTableNames = new string[] {
            "TestDataTable",
            "UIForm",
        };
        /// <summary>
        /// 加载标志,可以用来检测是否所有的资源都已经加载完毕
        /// </summary>
        private Dictionary<string,bool> m_LoadedFlag = new Dictionary<string, bool>();
        
        protected override void OnInit(IFsm<IProcedureManager> procedureOwner) {
            base.OnInit(procedureOwner);
        }

        protected override void OnEnter(IFsm<IProcedureManager> procedureOwner) {
            base.OnEnter(procedureOwner);
            GameEntry.Event.Subscribe(LoadDataTableSuccessEventArgs.EventId,OnLoadDataTableSuccess);
            GameEntry.Event.Subscribe(LoadDataTableFailureEventArgs.EventId,OnLoadDataTableFailure);
            // 执行预加载资源流程
            PreloadResources();
        }

        protected override void OnUpdate(IFsm<IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds) {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            // 检查预加载资源是否加载完毕
            foreach (KeyValuePair<string,bool> loadedFlag in m_LoadedFlag) {
                if (!loadedFlag.Value) {
                    return;
                }
            }
            // 如果加载完毕，那么就进入下一个流程
            ChangeState<ProcedureMain>(procedureOwner);
        }

        protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown) {
            base.OnLeave(procedureOwner, isShutdown);
            GameEntry.Event.Unsubscribe(LoadDataTableSuccessEventArgs.EventId,OnLoadDataTableSuccess);
            GameEntry.Event.Unsubscribe(LoadDataTableFailureEventArgs.EventId,OnLoadDataTableFailure);
        }

        /// <summary>
        /// 预加载资源
        /// </summary>
        private void PreloadResources() {
            // 加载DataTable
            foreach (var dataTableName in DateTableNames) {
                LoadDataTable(dataTableName);
            }
        }

        /// <summary>
        /// 加载数据表
        /// </summary>
        /// <param name="dataTableName"></param>
        private void LoadDataTable(string dataTableName) {
            string dataTableAssetName = AssetUtility.GetDataTableAsset(dataTableName, true);
            m_LoadedFlag.Add(dataTableAssetName,false);
            GameEntry.DataTable.LoadDataTable(dataTableName,dataTableAssetName,this);
        }

        private void OnLoadDataTableSuccess(object sender,GameEventArgs e) {
            LoadDataTableSuccessEventArgs ne = (LoadDataTableSuccessEventArgs) e;
            if (ne.UserData != this) {
                return;
            }

            m_LoadedFlag[ne.DataTableAssetName] = true;
            Log.Info("Load data table '{0}' OK.", ne.DataTableAssetName);
        }

        private void OnLoadDataTableFailure(object sender,GameEventArgs e) {
            LoadDataTableFailureEventArgs ne = (LoadDataTableFailureEventArgs)e;
            if (ne.UserData != this)
            {
                return;
            }

            Log.Error("Can not load data table '{0}' from '{1}' with error message '{2}'.", ne.DataTableAssetName, ne.DataTableAssetName, ne.ErrorMessage);
        }
    }
}
